<!-- 还愿动画 -->
<template>
	<u-overlay :show="show" @click="clickOverlay">
		<!-- 选择灯笼 -->
		<view
			class="huanyuan_content puff-in-center"
			v-if="showDl"
			:style="{
				width: clientWidth + 'px'
			}"
		>
			<view class="huanyuan_title">
				<image
					:src="imgUrl + 'imgs/worship/huanyuan_left.png'"
					style="margin-right: 16rpx"
					mode=""
				></image>
				请选择还愿灯笼
				<image
					:src="imgUrl + 'imgs/worship/huanyuan_right.png'"
					style="margin-left: 16rpx"
					mode=""
				></image>
			</view>
			<view class="huanyuan_dl">
				<view
					:class="['huanyuan_dl_img', activeIndex === 188 && 'active']"
					style="
						width: 230rpx;
						top: 0;
						left: 50%;
						transform: translateX(-50%);
						padding-top: 50rpx;
					"
					@click="activeIndex = 188"
				>
					<image
						:src="
							imgUrl +
							(activeIndex === 188 ? 'imgs/worship/dl2.png' : 'imgs/worship/dl1.png')
						"
						mode="widthFix"
					></image>
					188福币
				</view>
				<view
					:class="['huanyuan_dl_img', activeIndex === 38 && 'active']"
					style="
						width: 150rpx;
						left: 0;
						top: 200rpx;
						font-size: 24rpx;
						padding-top: 30rpx;
					"
					@click="activeIndex = 38"
				>
					<image
						:src="
							imgUrl +
							(activeIndex === 38 ? 'imgs/worship/dl2.png' : 'imgs/worship/dl1.png')
						"
						mode="widthFix"
					></image>
					38福币
				</view>
				<view
					:class="['huanyuan_dl_img', activeIndex === 688 && 'active']"
					@click="activeIndex = 688"
					style="width: 300rpx; left: 60rpx; top: 41rpx"
				>
					<image
						:src="
							imgUrl +
							(activeIndex === 688 ? 'imgs/worship/dl2.png' : 'imgs/worship/dl1.png')
						"
						mode="widthFix"
					></image>
					688福币
				</view>
				<view
					:class="['huanyuan_dl_img', activeIndex === 388 && 'active']"
					@click="activeIndex = 388"
					style="width: 300rpx; left: 440rpx; top: 10rpx"
				>
					<image
						:src="
							imgUrl +
							(activeIndex === 388 ? 'imgs/worship/dl2.png' : 'imgs/worship/dl1.png')
						"
						mode="widthFix"
					></image>
					388福币
				</view>
				<view
					:class="['huanyuan_dl_img', activeIndex === 68 && 'active']"
					style="
						width: 150rpx;
						right: 0;
						top: 180rpx;
						font-size: 24rpx;
						padding-top: 30rpx;
					"
					@click="activeIndex = 68"
				>
					<image
						:src="
							imgUrl +
							(activeIndex === 68 ? 'imgs/worship/dl2.png' : 'imgs/worship/dl1.png')
						"
						mode="widthFix"
					></image>
					68福币
				</view>
				<view
					:class="['huanyuan_dl_img', activeIndex === 8 && 'active']"
					style="
						width: 150rpx;
						left: 50rpx;
						top: 300rpx;
						font-size: 24rpx;
						padding-top: 30rpx;
					"
					@click="activeIndex = 8"
				>
					<image
						:src="
							imgUrl +
							(activeIndex === 8 ? 'imgs/worship/dl2.png' : 'imgs/worship/dl1.png')
						"
						mode="widthFix"
					></image>
					8福币
				</view>
				<view
					:class="['huanyuan_dl_img', activeIndex === 88 && 'active']"
					@click="activeIndex = 88"
					style="width: 200rpx; left: 170rpx; top: 370rpx; padding-top: 50rpx"
				>
					<image
						:src="
							imgUrl +
							(activeIndex === 88 ? 'imgs/worship/dl2.png' : 'imgs/worship/dl1.png')
						"
						mode="widthFix"
					></image>
					88福币
				</view>
				<view
					:class="['huanyuan_dl_img', activeIndex === 888 && 'active']"
					@click="activeIndex = 888"
					style="width: 320rpx; left: 300rpx; top: 300rpx"
				>
					<image
						:src="
							imgUrl +
							(activeIndex === 888 ? 'imgs/worship/dl2.png' : 'imgs/worship/dl1.png')
						"
						mode="widthFix"
					></image>
					888福币
				</view>
				<view
					:class="['huanyuan_dl_img', activeIndex === 18 && 'active']"
					@click="activeIndex = 18"
					style="
						width: 150rpx;
						right: 50rpx;
						top: 320rpx;
						font-size: 24rpx;
						padding-top: 30rpx;
					"
				>
					<image
						:src="
							imgUrl +
							(activeIndex === 18 ? 'imgs/worship/dl2.png' : 'imgs/worship/dl1.png')
						"
						mode="widthFix"
					></image>
					18福币
				</view>
			</view>
			<view
				v-if="ybCoinNumber >= activeIndex"
				class="huanyuan_btn"
				style="width: 400rpx"
				@click.native.stop="huanyuan"
			>
				还愿
			</view>
			<view
				v-else
				class="huanyuan_btn"
				style="width: 450rpx"
				@click="goUrl('/subPages/mine/cloudCoin/recharge')"
			>
				余额不足,去充值
			</view>
			<image
				class="huanyuan_close"
				@click="show = false"
				:src="imgUrl + 'imgs/worship/close.png'"
				mode=""
			></image>
		</view>

		<!-- 选择成功 -->
		<view
			class="huanyuan_success"
			:style="{
				top: navHeight ? `calc(${navHeight}px + 136rpx)` : '212rpx',
				width: clientWidth + 'px'
			}"
			v-if="!showDl"
		>
			<image :src="imgUrl + 'imgs/worship/dl_success.gif'" mode="widthFix"></image>
			<view class="huanyuan_success_content">
				感谢您的虔诚与支持，您的还愿已成功，愿此份心意化作无限的祝福，伴随您每一天。
			</view>
		</view>
	</u-overlay>
</template>

<script setup>
import { onLoad } from '@dcloudio/uni-app';
import { ref, reactive, watch } from 'vue';
import { imgUrl } from '@/external/utils/imgUrl.js';
import store from '@/store/index.js';
import WorshipApi from '@/API/worship';

const worshipApi = new WorshipApi();
const navHeight = getApp().globalData.navHeight;
const clientWidth = document?.body.clientWidth || uni.getSystemInfoSync().screenWidth;

const show = ref(false);
const showDl = ref(true); //是否展示灯笼
const activeIndex = ref(188);

const props = defineProps({
	worshipPeopleId: String, //人物id
	ybCoinNumber: Number //福币余额
});
const emits = defineEmits('finish');

/**
 * 点击去还愿
 */
const huanyuan = async () => {
	let res = await worshipApi.huanYuan({
		worshipPeopleId: props.worshipPeopleId,
		price: activeIndex.value
	});
	if (res.code === 0) {
		showDl.value = false;
		//5s自动关闭弹框
		setTimeout(() => {
			show.value = false;
		}, 5000);
		emits('finish');
	}
};

/**
 * 点击蒙层
 */
const clickOverlay = () => {
	if (!showDl.value) {
		show.value = false;
		setTimeout(() => {
			showDl.value = true;
		}, 1000);
	}
};

const goUrl = (url) => {
	uni.navigateTo({
		url: url
	});
};

watch(show, (newVal) => {
	if (newVal) {
		showDl.value = true;
		activeIndex.value = 188;
	}
});

defineExpose({
	show
});
</script>

<style lang="scss">
@import '/static/css/animation.css';
.huanyuan_content {
	left: 50%;
	top: 50%;
	transform: translate(-50%, -50%);
	position: absolute;
	width: 100%;
	display: flex;
	align-items: center;
	justify-content: center;
	flex-direction: column;

	.huanyuan_title {
		display: flex;
		align-items: center;
		justify-content: center;
		font-weight: bold;
		font-size: 32rpx;
		color: #ffedc8;
		line-height: 32rpx;
		letter-spacing: 2px;
		text-shadow: 0px 2px 2px rgba(0, 0, 0, 0.3);
		white-space: nowrap;

		image {
			width: 78rpx;
			height: 14rpx;
		}
	}

	.huanyuan_dl {
		margin-top: 50rpx;
		width: 100%;
		height: 536rpx;
		position: relative;
		background: url('http://112.124.50.179:8888/imgs/worship/dl_bgc.png') no-repeat;
		background-size: 100%;
		z-index: -2;

		.huanyuan_dl_img {
			width: 200rpx;
			height: 200rpx;
			position: absolute;
			display: flex;
			justify-content: center;
			padding-top: 10%;
			font-weight: bold;
			font-size: 27rpx;
			color: #ffeaae;
			line-height: 32rpx;
			box-sizing: border-box;

			image {
				position: absolute;
				width: 100%;
				top: 0;
				z-index: -1;
			}
		}

		.active {
			color: #0e0e0e;
		}
	}

	.huanyuan_btn {
		width: 450rpx;
		height: 100rpx;
		margin-top: 130rpx;
		background: url('http://112.124.50.179:8888/imgs/worship/huanyuan_btn.png') no-repeat;
		background-size: 100% 100%;
		font-weight: bold;
		font-size: 32rpx;
		color: #524630;
		line-height: 100rpx;
		letter-spacing: 1px;
		text-shadow: 0px 2px 2px rgba(0, 0, 0, 0.3);
		text-align: center;
	}

	.huanyuan_close {
		margin-top: 72rpx;
		width: 48rpx;
		height: 48rpx;
	}
}

.huanyuan_success {
	left: 50%;
	transform: translateX(-50%);
	position: fixed;
	width: 100%;
	display: flex;
	align-items: center;
	justify-content: center;
	flex-direction: column;

	image {
		width: 100%;
	}

	.huanyuan_success_content {
		margin: 32rpx 64rpx 0;
		font-weight: bold;
		font-size: 30rpx;
		color: #ffedc8;
		line-height: 45rpx;
		letter-spacing: 2px;
		text-shadow: 0px 2px 2px rgba(0, 0, 0, 0.3);
		text-stroke: 0px #e64229;
		text-align: center;
		font-style: normal;
		text-transform: none;
		-webkit-text-stroke: 0px #e64229;
	}
}
</style>
